import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.JSlider;
import javax.swing.JLabel;

public class Window extends JFrame implements MouseListener {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	Window frame;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Window frame = new Window();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	Random r = new Random();
int ideeczka=1;
	boolean periodyka=false;
	int l_x=500;
	int l_y=500;	
	Obszar Mapa= new Obszar(l_x,l_y);
	final panel panelek = new panel(Mapa);
	private JTextField txtLlosowych;
	JSlider slider = new JSlider();
	 int predkosc=1;
	 JLabel lblNewLabel = new JLabel("L.iteracji po 1 przycisku");
	public Window() {
		
		
	
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800,600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);	
		//panelek.setBackground(Color.WHITE);
		panelek.setBounds(10, 46, l_x, l_y);
		contentPane.add(panelek);
		panelek.setLayout(null); 
		panelek.addMouseListener(this);
		
		
		
		JButton btnStart = new JButton("Czysc");
		btnStart.setBounds(660, 11, 114, 23);
		contentPane.add(btnStart);
		
		JButton btnMoore = new JButton("Moore");
		btnMoore.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(!periodyka){
				for( int a=0; a<predkosc; a++)
				{
				Mapa.Moorebez();
				}
				panelek.revalidate();
				panelek.repaint();
				}
				else
				{
					for( int a=0; a<predkosc; a++)
					{
					Mapa.Moorez();
					}
					panelek.revalidate();
					panelek.repaint();
					}
							}
		});
		btnMoore.setBounds(660, 205, 114, 23);
		contentPane.add(btnMoore);
		
		JButton btnVonNeumann = new JButton("von Neumann");
		btnVonNeumann.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			if(!periodyka){
				for( int a=0; a<predkosc; a++)
				{
				Mapa.Vonbez();
				}
				panelek.revalidate();
				panelek.repaint();
				}
				else
				{
					for( int a=0; a<predkosc; a++)
					{
					Mapa.Vonz();
					}
					panelek.revalidate();
					panelek.repaint();
					}
							}
		});
		
		btnVonNeumann.setBounds(660, 239, 114, 23);
		contentPane.add(btnVonNeumann);
		
		JRadioButton rdbtnPeriodycznosc = new JRadioButton("Periodycznosc");
		rdbtnPeriodycznosc.setBounds(524, 11, 109, 23);
		contentPane.add(rdbtnPeriodycznosc);
		
		JButton btnNewButton = new JButton("Rownomiernie - 10 pkt");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Mapa.rownomiernie();
			}
		});
		btnNewButton.setBounds(660, 46, 114, 23);
		contentPane.add(btnNewButton);
		
		txtLlosowych = new JTextField();
		txtLlosowych.setText("L.losowych");
		txtLlosowych.setBounds(660, 80, 114, 20);
		contentPane.add(txtLlosowych);
		txtLlosowych.setColumns(10);
		
		JButton btnNewButton_1 = new JButton("Generuj Losowe");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String abc= txtLlosowych.getText();
				int a= Integer.parseInt(abc);
				Mapa.Losowe(a);
				panelek.revalidate();
				panelek.repaint();

			}
		});
		btnNewButton_1.setBounds(660, 111, 114, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnPentLewe = new JButton("Pent. Lewe");
		btnPentLewe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!periodyka){
					for( int a=0; a<predkosc; a++)
					{
					Mapa.PentagonalneLewebez();
					}
					panelek.revalidate();
					panelek.repaint();
					}
					else
					{
						for( int a=0; a<predkosc; a++)
						{
						Mapa.PentagonalneLewez();
						}
						panelek.revalidate();
						panelek.repaint();
						}
			}
		});
		btnPentLewe.setBounds(660, 273, 114, 23);
		contentPane.add(btnPentLewe);
		
		slider.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
			predkosc=slider.getValue();
			String tekst;
			tekst=Integer.toString(predkosc);
			lblNewLabel.setText("Liczba iteracji: "+tekst);
			}
			
		});
		
	

		JButton btnPentPrawe = new JButton("Pent. Prawe");
		btnPentPrawe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!periodyka){
					for( int a=0; a<predkosc; a++)
					{
					Mapa.PentagonalnePrawebez();
					}
					panelek.revalidate();
					panelek.repaint();
					}
					else
					{
						for( int a=0; a<predkosc; a++)
						{
						Mapa.PentagonalnePrawez();
						}
						panelek.revalidate();
						panelek.repaint();
						}
			}
		});
		btnPentPrawe.setBounds(660, 307, 114, 23);
		contentPane.add(btnPentPrawe);
		
		JButton btnPentGrne = new JButton("Pent. G\u00F3rne");
		btnPentGrne.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!periodyka){
					for( int a=0; a<predkosc; a++)
					{
					Mapa.PentagonalneGorabez();
					}
					panelek.revalidate();
					panelek.repaint();
					}
					else
					{
						for( int a=0; a<predkosc; a++)
						{
						Mapa.PentagonalneGoraz();
						}
						panelek.revalidate();
						panelek.repaint();
						}
			}
		});
		btnPentGrne.setBounds(660, 341, 114, 23);
		contentPane.add(btnPentGrne);
		
		JButton btnPentDolne = new JButton("Pent. Dolne");
		btnPentDolne.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!periodyka){
					for( int a=0; a<predkosc; a++)
					{
					Mapa.PentagonalneDolbez();
					}
					panelek.revalidate();
					panelek.repaint();
					}
					else
					{
						for( int a=0; a<predkosc; a++)
						{
						Mapa.PentagonalneDolz();
						}
						panelek.revalidate();
						panelek.repaint();
						}
			}
		});
		btnPentDolne.setBounds(660, 375, 114, 23);
		contentPane.add(btnPentDolne);
		
		JButton btnHeksLewaGra = new JButton("Heks. Lewa G\u00F3ra");
		btnHeksLewaGra.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!periodyka){
					for( int a=0; a<predkosc; a++)
					{
					Mapa.HeksaLewaGoraBez();
					}
					panelek.revalidate();
					panelek.repaint();
					}
					else
					{
						for( int a=0; a<predkosc; a++)
						{
						Mapa.HeksaLewaGoraz();
						}
						panelek.revalidate();
						panelek.repaint();
						}
			}
		});
		btnHeksLewaGra.setBounds(660, 409, 114, 23);
		contentPane.add(btnHeksLewaGra);
		
		JButton btnHeksLewyD = new JButton("Heks. Lewy D\u00F3\u0142");
		btnHeksLewyD.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!periodyka){
					for( int a=0; a<predkosc; a++)
					{
					Mapa.HeksaLewaDolBez();
					}
					panelek.revalidate();
					panelek.repaint();
					}
					else
					{
						for( int a=0; a<predkosc; a++)
						{
						Mapa.HeksaLewaDolz();
						}
						panelek.revalidate();
						panelek.repaint();
						}
			}
		});
		btnHeksLewyD.setBounds(660, 443, 114, 23);
		contentPane.add(btnHeksLewyD);
		
		
		slider.setBounds(660, 168, 114, 26);
		contentPane.add(slider);
		
		
		lblNewLabel.setBounds(660, 145, 114, 14);
		contentPane.add(lblNewLabel);
		
		
		
		rdbtnPeriodycznosc.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				periodyka=!periodyka;
			//	System.out.println(periodyka);
			}
		});
		
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Mapa.czysc();
				panelek.revalidate();
				panelek.repaint();
			}
		});			

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		int a=e.getX();
		int b=e.getY();
		int gr=r.nextInt(255);
		int gg=r.nextInt(255);
		int gb=r.nextInt(255);
		
		this.Mapa.tablica[a][b]=new Ziarno(ideeczka++,a,b,gr,gg,gb,true);			//WYSWIETLA MI SIE KROPKA I HUK.. nie wiem czemu
	
		System.out.println("NOWE ZIARNO ID:  "+ this.Mapa.tablica[a][b].id);
	panelek.revalidate();
	panelek.repaint();

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}

